package school30.AppFileManager;

import java.util.*;
import javax.microedition.lcdui.*;
import school30.*;
import school30.io.File;
import school30.io.FileManager;
import school30.io.FileReader;
import school30.io.FileWriter;
import school30.io.fs.FileSystemConnector;
import school30.io.memory.MemoryFileConnector;
import school30.io.memory.MemoryFileStorage;
import school30.ui.*;

public class AppFileManager extends School30App {

    public Object lock = new Object();
    public boolean destroying = false;
    public FileManager fm = new FileManager(new Hashtable());
    public File curDir = new File("/");
    private int selectedItem;
    private int itemColor = 0x000000;
    private int dirColor = 0xff0000;
    private int selectedItemColor = 0x0000ff;
    private int curDirColor = 0x00ff00;
    private int sizeColor = 0xff8888;
    private long curSize = -2;
    private String[] dirContent;

    public AppFileManager() {
        destroying = false;
        new Thread() {

            public void run() {
                fm.mount("memory", new MemoryFileConnector(new MemoryFileStorage()));
                fm.mount("root", new FileSystemConnector());
            }
        }.start();
    }

    private void init() {
        new Thread() {

            public void run() {
                fm.createFolder(new File("/memory/user"));
                fm.createFolder(new File("/memory/system"));
                fm.createFolder(new File("/memory/user/docs"));
                fm.createFolder(new File("/memory/user/pics"));
                fm.createFolder(new File("/root/root1/a"));
                FileWriter fw = fm.openWrite(new File("/memory/editme.txt"));
                fw.write("Hello, World!...................".getBytes());
                fw.close();
            }
        }.start();
    }

    public void startApp() {
        init();
        final Painter p = getPainter();
        synchronized (lock) {
            while (!destroying) {
                new Thread() {

                    public void run() {
                        p.setColor(0xffffff);
                        p.fillRectangle(0, 0, getWidth(), getHeight());
                        p.setColor(curDirColor);
                        p.drawString(curDir.path(), 0, 0, Graphics.TOP | Graphics.LEFT);
                        dirContent = fm.folderContents(curDir);
                        for (int i = 0; i < dirContent.length; i++) {
                            if (selectedItem == i) {
                                p.setColor(selectedItemColor);
                            } else {
                                if (fm.size(curDir.getChild(dirContent[i])) == -2) {
                                    p.setColor(dirColor);
                                } else {
                                    p.setColor(itemColor);
                                }
                            }
                            p.drawString(dirContent[i], 10, i * 20 + 20, Graphics.TOP | Graphics.LEFT);
                        }
                        if (dirContent.length > 0) {
                            p.setColor(sizeColor);
                            curSize = fm.size(curDir.getChild(dirContent[selectedItem]));
                            if (curSize != -2) {
                                p.drawString("Size: " + curSize,
                                        0, getHeight(), Graphics.BOTTOM | Graphics.LEFT);
                            } else {
                                p.drawString("DIR",
                                        0, getHeight(), Graphics.BOTTOM | Graphics.LEFT);
                            }

                            if (curSize != -2 &&
                                    dirContent[selectedItem].endsWith(".txt")) {
                                FileReader fr = fm.openRead(
                                        curDir.getChild(dirContent[selectedItem]));
                                byte[] b = new byte[20];
                                int br = (int) fr.read(b, b.length);
                                fr.close();
                                p.drawString("Preview: " + new String(b, 0, br), 0, getHeight() - 20,
                                        Graphics.BOTTOM | Graphics.LEFT);
                            }
                        }
                    }
                }.start();
                try {
                    lock.wait();
                } catch (Exception ex) {
                }
            }
            System.out.println("Destroying!!!!");
        }
    }

    public void terminateApp() {
        destroying = true;
        lock.notifyAll();
    }

    public void keyPressed(int kc) {
        if (kc == Canvas.KEY_NUM5) {
            if (curSize == -2) {
                curDir = curDir.getChild(dirContent[selectedItem]);
                selectedItem = 0;
                synchronized (lock) {
                    lock.notifyAll();
                }
            }
        } else if (kc == Canvas.KEY_STAR) {
            if (!curDir.isRoot()) {
                curDir = curDir.getParent();
                selectedItem = 0;
                synchronized (lock) {
                    lock.notifyAll();
                }
            }

        } else if (kc == Canvas.KEY_NUM2) {
            selectedItem--;
            if (selectedItem < 0) {
                selectedItem = dirContent.length - 1;
            }
            synchronized (lock) {
                lock.notifyAll();
            }
        } else if (kc == Canvas.KEY_NUM8) {
            selectedItem++;
            if (selectedItem >= dirContent.length) {
                selectedItem = 0;
            }
            synchronized (lock) {
                lock.notifyAll();
            }
        }
    }

    public void keyReleased(int kc) {
    }
}
